package com.ddpyjqtd.thread.threadstatus;

import java.util.concurrent.TimeUnit;

/**
 * 
 * @Description: 线程的当前状态 <br>
 *               查看当前线程状态： 运行该 main 方法，在cmd窗口中 输入 命令： jps,获取到pid ,<br>
 *               再输入 jstack pid 即可查看到当前线程的状态<br>
 * 
 * @author: ddpyjqtd
 * @Date: 2019年5月13日
 *
 */
public class ThreadStatus extends Thread {

	@Override
	public void run() {
		super.run();
	}

	public static void main(String[] args) {
		Thread.currentThread().setName("这个是主线程");
		State state = Thread.currentThread().getState();
		System.out.println(state);
		// Thread.State.NEW;
		// Thread.State.RUNNABLE;
		// Thread.State.WAITING;
		// Thread.State.TIMED_WAITING;
		// Thread.State.BLOCKED;
		// Thread.State.TERMINATED;

		new Thread(new Runnable() {
			public void run() {
				try {
					TimeUnit.SECONDS.sleep(300);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "THREAD_SLEEP").start();

		new Thread(new Runnable() {
			public void run() {
				synchronized (this) {

					try {
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}, "THREAD_WAIT").start();

		new Thread(new Runnable() {
			public void run() {
				yield();
			}
		}, "THREAD_YIELD").start();

		Runnable runnable = new Runnable() {
			public void run() {
				synchronized (this) {
					while (true) {

					}
				}
			}
		};
		Thread t1 = new Thread(runnable, "SYNCHRONIZED_T1");
		Thread t2 = new Thread(runnable, "SYNCHRONIZED_T2");
		t1.start();
		t2.start();

	}

}
